package test.lamda;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.omg.PortableInterceptor.INACTIVE;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Sample {

    List<Data> list;
    @Before
    public void init(){
        ThreadLocalRandom random = ThreadLocalRandom.current();
        list = new ArrayList<>();
        for (int i=0;i<10;i++){
            Data data = new Data(random.nextInt(0,255), random.nextInt(0,255),random.nextInt(0,255),random.nextInt(0,1),"x"+i);
            list.add(data);
        }
    }

    @Test
    public void print(){
        list.stream().forEach(System.out::println);
    }
    // 交集
    @Test
    public void andTest(){
        List<Integer> arr1 = Arrays.asList(1,2,3,4,5);
        List<Integer> arr2 = Arrays.asList(2,3,4);

        List<Integer> result = arr1.stream().filter(arr2::contains).collect(Collectors.toList());
        Assert.assertEquals(3,result.size());
    }

    // left join
    @Test
    public void leftJoinTest(){
        List<Integer> arr1 = Arrays.asList(1,2,3,4,5);
        List<Integer> arr2 = Arrays.asList(2,3,4);

        List<Integer> result = arr1.stream().filter(arr2::contains).collect(Collectors.toList());
        Assert.assertEquals(3,result.size());
    }


}

class Data{
    Integer x1,x2,x3;
    Integer y;
    String str;

    public Integer getX1() {
        return x1;
    }

    public Integer getX2() {
        return x2;
    }

    public Integer getX3() {
        return x3;
    }

    public Integer getY() {
        return y;
    }

    public String getStr() {
        return str;
    }

    public Data(Integer x1, Integer x2, Integer x3, Integer y, String str) {
        this.x1 = x1;
        this.x2 = x2;
        this.x3 = x3;
        this.y = y;
        this.str = str;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("{");
        sb.append("\"x1\":")
                .append(x1);
        sb.append(",\"x2\":")
                .append(x2);
        sb.append(",\"x3\":")
                .append(x3);
        sb.append(",\"y\":")
                .append(y);
        sb.append(",\"str\":\"")
                .append(str).append('\"');
        sb.append('}');
        return sb.toString();
    }
}